//app.js
App({
  data: {
    DeviceCode: "", // 设备编码
    isconnect: false, //当前手机是否连接设备
    isopenble: false, // 当前手机蓝牙是否打开
    bleMac: [], //扫描到的设备   (为了方便处理ios)
    connectMac: "", //当前手机连接的设备.
    connectindex: "0" // ble连接次数
  },

  onLaunch: function () {
    // wx.removeStorageSync('store')
    this.init()
    this.globalData.sysinfo = wx.getSystemInfoSync();
  },

  watchCallBack: {},
  watchingKeys: [],

  /** 初始化 */
  init: function () {
    // 全局数据
    let storeData = wx.getStorageSync('store')

    console.log('全局数据', storeData)

    if (storeData) {
      storeData.connected = false
      storeData.deviceId = null
      storeData.name = null
      storeData.timeout= true,
      storeData.serviceId = null
      storeData.lockId = null
      storeData.temperature = '0'
      storeData.useStatus = 2
      storeData.binding = false
      storeData.changePalte = false
      storeData.isBinding = false
      storeData.canBind = false
      storeData.plateList = storeData.plateList.map(item => {
        let _item = item
        _item.isFocus = false
        return _item
      })
      this.globalData = storeData
    } else {
      storeData = this.globalData
    }
    this.$globalData = Object.assign({}, storeData);
  },

  /** 设置全局数据 */
  setGlobalData: function (obj) {
    Object.keys(obj).map(key => {
      this.globalData[key] = obj[key];
    });
    wx.setStorageSync('store', this.globalData) // 加入缓存
  },

  /** watch函数 */
  $watch(key, cb) {
    this.watchCallBack = Object.assign({}, this.watchCallBack, {
      [key]: this.watchCallBack[key] || []
    });
    this.watchCallBack[key].push(cb);
    if (!this.watchingKeys.find(x => x === key)) {
      const that = this;
      this.watchingKeys.push(key);
      Object.defineProperty(this.globalData, key, {
        configurable: true,
        enumerable: true,
        set: function (val) {
          const old = that.$globalData[key];
          that.$globalData[key] = val;
          that.watchCallBack[key].map(func => func(val, old));
        },
        get: function () {
          return that.$globalData[key];
        }
      });
    }
  },
  getModel: function () { //获取手机型号
    return this.globalData.sysinfo["model"]
  },
  getWxVersion: function () { //获取微信版本号
    return this.globalData.sysinfo["version"]
  },
  getSystem: function () { //获取操作系统版本
    return this.globalData.sysinfo["system"]
  },
  getPlatform: function () { //获取客户端平台
    return this.globalData.sysinfo["platform"]
  },
  getSDKVersion: function () { //获取客户端基础库版本
    return this.globalData.sysinfo["SDKVersion"]
  },

  //toast提示
  toastTips: function (txt) {
    wx.showToast({
      title: txt
    })
  },
  //toast提示，可以设置等待时长
  toastTips1: function (txt, time) {
    wx.showToast({
      title: txt,
      duration: time
    })
  },
  toastTips2: function (txt) {
    wx.showToast({
      title: txt,
      icon: "loading"
    })
  },

  //弹窗提示
  showModal: function (txt) {
    wx.showModal({
      title: '提示',
      content: txt,
      showCancel: false,
    })
  },
  //弹窗提示,有确认按钮
  showModal1: function (txt) {
    wx.showModal({
      title: "提示",
      content: txt,
      showCancel: false,
      confirmText: "确定"
    })
  },
  //loading
  showLoading: function (txt) {
    wx.showLoading({
      title: txt,
      mask: true
    });
  },

  isBlank: function (str) {
    if (Object.prototype.toString.call(str) === '[object Undefined]') { //空
      return true
    } else if (
      Object.prototype.toString.call(str) === '[object String]' ||
      Object.prototype.toString.call(str) === '[object Array]') { //字条串或数组
      return str.length == 0 ? true : false
    } else if (Object.prototype.toString.call(str) === '[object Object]') {
      return JSON.stringify(str) == '{}' ? true : false
    } else {
      return true
    }
  },

  globalData: {
    isopen: false, //蓝牙适配器是否已打开
    devices: [],
    connected: false,
    timeout: true,
    // chs: [],
    deviceId: "",
    name: "", // 蓝牙名称
    allRes: "",
    serviceId: "",
    writeCharacteristicId: '',
    notifyCharacteristicId: '',
    lockId: '', // 锁具ID
    temperature: '0', //温度
    plateIdx: 0, // 车牌管理
    useStatus: 2, // 使用状态 1 正确 2错误 3 绑定
    binding: false,
    changePalte: false,
    isBinding: false,
    canBind: false,
    plateList: [{
        tips: "车牌一",
        id: 1,
        alias: '浙',
        eName: 'ZheJiang',
        plate: 'B0000B',
        password: '12345678',
        lockId: null,
        checked: true,
        devId: null,
        energy: false,
        isFocus: false
      },
      {
        tips: "车牌二",
        id: 2,
        alias: '浙',
        eName: 'ZheJiang',
        plate: 'B0000B',
        password: '12345678',
        lockId: null,
        checked: false,
        devId: null,
        energy: false,
        isFocus: false
      },
      {
        tips: "车牌三",
        id: 3,
        alias: '浙',
        eName: 'ZheJiang',
        plate: 'B0000B',
        password: '12345678',
        lockId: null,
        checked: false,
        devId: null,
        energy: false,
        isFocus: false
      }
    ],
  }
})